package ywpt.web.action.system;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ywpt.core.web.BaseAction;
import ywpt.model.SystemHelp;
import ywpt.service.system.SystemHelpService;

public class SystemHelpAction extends BaseAction implements ModelDriven<SystemHelp> ,Preparable{

	/**
	 * ϵͳ系统帮助Action
	 */
	private static final long serialVersionUID = 8391489702539261881L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private static final String URL="/sysHelp/";
	private Long id;
	private SystemHelp systemHelp;
	private List<SystemHelp> systemHelpList = new ArrayList<SystemHelp>(); 
	private SystemHelpService systemHelpService;
	
	// �ļ����ϴ�
	private File[] upload;// ʵ���ϴ��ļ�
	private String[] uploadFileName; // �ϴ��ļ���
	private String[] uploadContentType; // �ļ�����������	

	//�ļ�������
	private String fileName;
	private String fileRealName;	
	
	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public SystemHelp getSystemHelp() {
		return systemHelp;
	}

	public void setSystemHelp(SystemHelp systemHelp) {
		this.systemHelp = systemHelp;
	}

	public List<SystemHelp> getSystemHelpList() {
		return systemHelpList;
	}

	public void setSystemHelpList(List<SystemHelp> systemHelpList) {
		this.systemHelpList = systemHelpList;
	}

	public SystemHelpService getSystemHelpService() {
		return systemHelpService;
	}

	public void setSystemHelpService(SystemHelpService systemHelpService) {
		this.systemHelpService = systemHelpService;
	}

	//------------------------------------------------------------------------------------------------------
	@Override
	public SystemHelp getModel() {
		return this.systemHelp;
	}

	@Override
	public void prepare() throws Exception {
		if (id!=null) {
			this.systemHelp = this.systemHelpService.getSystemHelpById(id);
		} else {
			this.systemHelp = new SystemHelp();
		}
	}
//======================================================================================================
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(SystemHelp.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = systemHelpService.getSystemHelpCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.setProjection(null);
		systemHelpList.addAll(systemHelpService.getSystemHelps(query, pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}
	
	/**
	 *�ϴ����µĸ��� 
	 */
	public String upload() throws Exception {
		try {
			String url = ServletActionContext.getServletContext().getRealPath(URL);
				File file_date = new File(url);
				if(!file_date.exists()){
					file_date.mkdirs();
				}	
			for (int i = 0; i < upload.length; i++) {
				if(upload[i].exists()){
					String type = uploadContentType[i]; // �ļ�����������
					String fileName = uploadFileName[i];
					String realName = UUID.randomUUID().toString() + getExt(fileName);
					File target = new File(url, realName);
					FileUtils.copyFile(upload[i], target);
					
					SystemHelp sh = new  SystemHelp();
					sh.setFileName(fileName);
					sh.setContentType(type);
					sh.setRealName(realName);
					this.systemHelpService.saveSystemHelp(sh);
				}
			}
			this.addActionMessage("附件上传成功!");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("上传失败:" + e.getMessage());
			return SUCCESS;
		}				
		
	}	
	
	public static String getExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}
	/**
	 *附件下载
	 */
	public String download(){
		return SUCCESS;
	}
	
	public void setFileName()
	{
		// �õ��������ص��ļ���
		systemHelp = this.systemHelpService.getSystemHelpById(id);
		this.fileName = systemHelp.getRealName(); //uuid������
		this.fileRealName = systemHelp.getFileName();//�ļ��ϴ���ʱ���ԭ��������
	}

	/*
	 * @getFileName �˷�����Ӧ����struts.xml�ļ��еģ� 
	 * <param name="contentDisposition">attachment;filename="${fileName}"</param>
	 * ����������õ������ع��������ļ�ʱ��ʾ���ļ��� Ҫ����ȷ����ʾ�����ļ���������Ҫ��fileName�ٴα���
	 * �����������ļ����������룬���޷����ص����
	 */
	public String getDownloadFileName() throws Exception
	{
		return new String(fileRealName.getBytes( ),"ISO-8859-1");
	}

	/*
	 * @getDownloadFile �˷�����Ӧ����struts.xml�ļ��еģ� <param
	 * name="inputName">downloadFile</param> ���������ļ����������Բο�struts2��Դ��
	 */
	public InputStream getDownloadFile()
	{
		this.setFileName();
		return ServletActionContext.getServletContext().getResourceAsStream(URL + fileName);
	}
	
	
	
	
	
	
	
	
}
